<?php

class Admin_Model_AclAdmin extends Zend_Acl {

    public function __construct($userLogin, array $permissions) {
        try {
            $dopAccess = array(
                0 => array( 0 => 'login', 1 => 'index',),
                1 => array( 0 => 'logout', 1 => 'index',),
                2 => array( 0 => 'error', 1 => 'index',),
                3 => array( 0 => 'index', 1 => 'index',),
                5 => array( 0 => 'admin-users', 1 => 'slam-remember',),
                6 => array( 0 => 'admin-users', 1 => 'refresh-info-bar',),
                7 => array( 0 => 'billing', 1 => 'add-to-history',),
            );
            
            $permissions =  array_merge($dopAccess, $permissions);
            // add role(user login)
            $this->addRole(new Zend_Acl_Role($userLogin));

            $resurce = array();
            foreach ($permissions as $perm)
                $resurce[$perm[0]] = 1;

            // add resource
            foreach ($resurce as $key => $res) {
                $this->add(new Zend_Acl_Resource($key));
            }
            // add allow permissions
            foreach ($permissions as $perm) {
                $this->allow($userLogin, $perm[0], $perm[1]);
            }
        } catch (Exception $ex) {
            die($ex->getMessage());
        }
    }

    public function isAllow($user, $resource, $action) {
        $result = false;
        try {
            $result = $this->isAllowed($user, $resource, $action);
        } catch (Exception $exc) {
            return false;
        }
        return $result;
    }

    public static function isPerm($resource, $action){
        
        $admin_account = new Zend_Session_Namespace('admin_account');
        if ($admin_account->role_id === 1 ) // admin
            return true;
        return Zend_Registry::get('ACL')->isAllow($admin_account->login, $resource, $action);
    }

}